/*	Primer v1 - program to find prime numbers.
 *  main.cpp
 *
 *	Discontinued: program too big, too much.
 *		It has good stuff, but its too complex.
 *  
 *	~Xevious 2-9-09
 */
#include <cstdlib>
#include <iostream>
#include <math.h>

using namespace std;

bool checkprime(unsigned long number);
void findprime();
void findxprimes(int xprimes);

unsigned long int primes[100000];
int numberofprimes = 0;
unsigned long highnumber = 10000000;
int maxprimes = 0;

int main()
{
    system("cls");
    system("title Prime Tool");
    int donow;
    cout << "Options:\n1. Check if n is prime.\n2. Find all primes up to n.\n3. Find n primes.\n" << endl;
    cout << "What shall I do? ";
    cin >> donow;
    cout << endl;
    
    switch(donow)
    {
        case 1:
             int check;
             cout << "What number? ";
             cin >> check;
             if (checkprime(check))
             {
                 cout << "Number (" << check << ") is prime." << endl;
                 break;
             }
             else
             {
                  cout << "Number(" << check << ") is not prime." << endl;
                  break;
             }
             cout << checkprime(check) << endl;
             break;
        case 2:
             cout << "Please enter ceiling number: ";
             cin >> highnumber;
             findprime();
             cout << "\nI found " << numberofprimes << " prime numbers while counting up to " << highnumber << ". ";
             cout << "They are:" << endl;
             for (int x = 0; x < numberofprimes; ++x)
             {
                /* if (x % 10 == 0)
                 {
                       cout << "\n";
                 }*/  // this if statement will break off the primes into chunks of 10.
                 cout << primes[x] << " ";
             }
             cout << "\n\n";
             break;
        case 3:
             int findprimes;
             cout << "How many primes shall I find? ";
             cin >> findprimes;
             findxprimes(findprimes);
             cout << "\nThe first " << findprimes << " primes are:\n";
             for (int x = 0; x < numberofprimes; ++x)
             {
                 cout << primes[x] << " ";
             }
             break;
             
    }
    cout << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

bool checkprime(unsigned long number) // this function will test if a number is a prime number
{									  // or not. Boolean.
    if (number == 2) // added this to align my results with online tables.
    {
        return true;
    }
    else if (number % 2 == 0) // if number is divisible by 2, then it's even.
    {
        return false;
    }
    
    else
    {
        int factorhigh = int(sqrt(number)+1); // highest number to check against.
        int divisor = 3; // start dividing by 3.
        
        while (divisor <= factorhigh) // check for division.
        {
              if (number % divisor == 0)
              {
                         return false;
              }
              divisor += 2;
        }
        
        return true; // well if it got here then it must be a prime.
   }
}

void findprime() // this function finds, counts, and stores primes.
{
     unsigned long primetest = 2;
     for (int i = 0; primetest <= highnumber; (primetest == 2) ? primetest = 3 : primetest += 2)
     {
         if (checkprime(primetest))
         {
             primes[i] = primetest;
             ++i;
             numberofprimes += 1;
         }
         
     }
     
}

void findxprimes(int xprimes) // this function will find primes up to user specified xprimes.
{
     do
     {
         findprime();
     } while (numberofprimes <= xprimes);
}
